<?php 

	include 'config.php';

	/**
	*	定义Model类
	*/

	class Model
	{
		//成员属性
		protected $tableName;
		protected $link;
		protected $pk;
		protected $fields = array();
		//存储字段信息
		protected $field;
		//存储排序的信息
		protected $order;
		//存储条件信息
		protected $where;
		//存储limit信息
		protected $limit;

		//构造方法
		public function __construct($tableName)
		{
			$this->tableName = $tableName;
			//链接数据库
			$this->link = mysqli_connect(HOST,USER,PWD);
			//选择数据库
			mysqli_select_db($this->link,DBNAME);
			//设置字符集
			mysqli_set_charset($this->link,CHARSET);
			//获取有效字段
			$this->getField();
		}
		/**
		*	添加数据
		*	array('username'=>'dsfds','password'=>'21432','age'=>'34');
		*
		*	insert into user(username,password,age) values ('wwe','34dsfdsfe','45');
		*/
		public function insert($arr)
		{
			//遍历数组
			foreach($arr as $k=>$v) {
				//判断有效字段
				if(!in_array($k,$this->fields)) {
					//不是有效字段进行删除
					unset($arr[$k]);
				}

			}
			//准备sql语句
			$sql= "insert into {$this->tableName}";
			//获取字段名
			$key = array_keys($arr);
			//转换字符串
			$key_str = "(".implode(',',$key).")";
			//获取值
			$value = array_values($arr);
			//把值转换成字符串
			$value_str = "('".implode("','",$value)."')";
			//拼接sql语句
			$sql = $sql.$key_str.' values '.$value_str;

			//执行sql
			$result = mysqli_query($this->link,$sql);

			if($result){

				return mysqli_insert_id($this->link);

			} else {
				 return false;
			} 
		}
		/*
		*	获取有效字段
		*/
		private function getField()
		{
			//准备sql语句
			$sql = "desc ".$this->tableName;
			//执行sql
			$result = mysqli_query($this->link,$sql);
			if($result) {
				//遍历数据
				while($row = mysqli_fetch_assoc($result)) {
					// 判断
					if($row['Key'] == 'PRI') {
						//获取id
						$this->pk = $row['Field'];
					} else {
						//获取数据表里的有效字段
						$this->fields[] = $row['Field'];
					}
					
				}
			}
		}
		/**
		*	删除数据
		*/
		public function delete($id)
		{
			//准备sql语句
			$sql = "delete from {$this->tableName} where {$this->pk} = {$id}";
			//执行sql语句
			$result = mysqli_query($this->link,$sql);
			if($result) {

				return mysqli_affected_rows($this->link);
			} else {

				return false;
			}
		}
		/**
		*	修改数据
		*
		* array('username'=>'dsfds','password'=>'21432','age'=>'34','id'=>3);
		*	update user set username='jjj',password='wre3245',age='343' where id = 3;
		*/
		public function update($arr)
		{
			//遍历数组
			foreach($arr as $k=>$v) {
				//判断有效字段
				if(!in_array($k,$this->fields) && $this->pk != $k) {
					//不是有效字段进行删除
					unset($arr[$k]);
				}

			}
			//准备sql语句
			$sql = "update {$this->tableName} set ";
			//遍历
			//定义空字符串 放值
			$str = '';
			//存放where条件 id
			$where = '';

			foreach($arr as $k=>$v)
			{
				//判断
				if($this->pk != $k){

					$str .= $k.'='."'$v'".',';
				} else {

					$where = " where {$this->pk} = ".$v;
				}
			}
			//去掉逗号
			$strs = rtrim($str,',');
			if(empty($where)) {

				die('修改条件不符合,请检查');
			}
			//拼接sql语句
			$sql = $sql.$strs.$where;
			//执行sql语句
			$result = mysqli_query($this->link,$sql);
			if($result) {

				return mysqli_affected_rows($this->link);
			} else {

				return false;
			}
		}

		/**
		*	设置查询字段条件
		*/
		public function field($field)
		{
			$this->field = $field;
			return $this;
		}
		/**
		*	设置排序的条件
		*/
		public function order($order)
		{
			$this->order = $order;
			return $this;
		}
		/**
		*	where 条件
		*/
		public function where($where)
		{
			$this->where = $where;
			return $this;
		}
		/**
		*	limit条件
		*/
		public function limit($limit)
		{
			$this->limit = $limit;
			return $this;
		}
		/**
		*	查询数据 
		*	select username,password,age from user where id>50 order by id desc  limit(3,5);
		*
		*
		*/
		public function select()
		{
			//获取字段信息
			$field_str = '';
			if(empty($this->field)) {

				$field_str = '*';
			} else {
				$field_str = $this->field;
			}
			//获取排序的信息
			$order_str = '';
			if(!empty($this->order)){

				$order_str = ' order by '.$this->order;
			}

			//获取条件信息
			$where_str = '';
			if(!empty($this->where)) {

				$where_str = ' where '.$this->where;
			}

			//获取limit信息
			$limit_str = '';
			if(!empty($this->limit)){

				$limit_str = ' limit '.$this->limit;
			}

			//准备sql语句
			$sql = "select {$field_str} from ".$this->tableName.$where_str.$order_str.$limit_str;
			//清除条件
			$this->where = '';
			$this->field = '';
			$this->order = '';
			$this->limit = '';

			//执行sql语句
			$result = mysqli_query($this->link,$sql);
			if($result) {
				//定义空数组 存储数据
				$arr = array();
				//遍历
				while($row = mysqli_fetch_assoc($result)) {
					//存数据
					$arr[] = $row;

				}
				//把数据进行返回
				return $arr;
			} else {
				return false;
			}
		}
		/**
		*	查询单条数据
		*/
		public function find($id)
		{
			//准备sql语句
			$sql ="select * from {$this->tableName} where {$this->pk} = {$id}";
			//执行sql语句
			$result = mysqli_query($this->link,$sql);

			if($result) {

				return mysqli_fetch_assoc($result);
			} else {

				return false;
			}
		}

		/**
		*	获取总数据
		*/
		public function count()
		{
			//准备sql语句
			$sql = "select count(*) as total from {$this->tableName}";
			//执行sql语句
			$result = mysqli_query($this->link,$sql);
			if($result) {
				//获取总数据
				$row = mysqli_fetch_assoc($result); 

				return $row['total'];				
			} else {

				return false;
			}
		}

		//关闭数据库
		public function __destruct()
		{
			mysqli_close($this->link);
		}
	}
	

	//实例化对象
	//$model = new Model('user');
	
	//数组
	//$arr = array('username'=>'ewrew','password'=>'214324','age'=>'34','abc'=>'abc');

	//添加数据
	//var_dump($model->insert($arr));

	//删除数据
	//echo $model->delete(5);

	//修改数据
	//$arr = array('username'=>'wqw','password'=>'23fe','age'=>'50','classid'=>'lamp161','id'=>7);

	//var_dump($model->update($arr));

	//查询数据
	// $str = $model->field('username,password')->order('id desc')->limit('3,5')->select();

	// var_dump($str);
	// echo '<hr>';
	// $strs = $model->select();

	// var_dump($strs);

	//查询单条数据
	// $str = $model->find(20);

	// var_dump($str);

	//获取总数据
	//echo $model->count();

	// echo $model->pk;

	// var_dump($model->fields);



